﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using Andromeda.Base.Application.UI.Windows.Forms;
using Andromeda.Forge.Common;
using ForgeIt.Forms;
using ForgeIt.Forms.Solution;
using TestEnvironment.Properties;

namespace TestEnvironment
{
    public partial class MainForm : BaseForm
    {
        private static bool _loaded;

        static readonly ForgeSolutionManager Manager = new ForgeSolutionManager();
        static readonly BackgroundWorkerLogForm BkgLog = new BackgroundWorkerLogForm();
        static readonly BackgroundWorker Worker = new BackgroundWorker();

        private static FOSolution Solution { get { return FOSolution.Instance; } }

        public MainForm()
        {
            if (_loaded) throw new NotSupportedException("This form is to run as a singleton");

            _loaded = true;

            InitializeComponent();

            Worker.DoWork += WorkerDoWork;

            Worker.ProgressChanged += WorkerProgressChanged;

            Worker.RunWorkerCompleted += WorkerRunWorkerCompleted;
            
            Worker.WorkerReportsProgress = true;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            
            
            Worker.RunWorkerAsync();

            if (BkgLog.ShowDialog(this) != DialogResult.OK) return;
            
            Manager.DataBind(Solution);
            
            if (!Manager.Visible) Manager.Show(this);
        }

        static void WorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BkgLog.AllowClose = true;
        }

        static void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            BkgLog.Log(e.UserState.ToString(), true);
            GC.Collect();
        }

        void WorkerDoWork(object sender, DoWorkEventArgs e)
        {
            FOSolution.ResetOperationLog += FOSolution_ResetOperationLog;
            FOSolution.Reset("DefaultName");
        }

        void FOSolution_ResetOperationLog(object sender, string message, EventArgs args)
        {
            Worker.ReportProgress(1, message);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            globalOpenFileDialog.Filter = Resources.Files_Frgsol_File_Filter;
            if (globalOpenFileDialog.ShowDialog() != DialogResult.OK) return;
            FOSolution.LoadSolution(globalOpenFileDialog.FileName);
            Manager.DataBind(Solution);
            if (!Manager.Visible) Manager.Show(this);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Solution.Save();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (globalFolderBrowserDialog.ShowDialog() != DialogResult.OK) return;
            Solution.Path = globalFolderBrowserDialog.SelectedPath;
            saveToolStripMenuItem_Click(sender, e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
